<?
session_start();
if(!(isset($_SESSION['usuario']))) {
    echo "AREA RESTINGIDAD";
} else { ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Orden de Compra</title>
        <link href="estilacho.css" rel="stylesheet" type="text/css">
        <script type="text/javascript" src="validarnumero.js"></script>
        <script type="text/javascript">

            function habilitar_plazoentregaespecifico(form){
                form.plazoentregaespecifico.disabled = false;
                form.plazoentregaespecifico.value = "Especifique";
            }
            function limpiar_plazoentregaespecifico(form){
                form.plazoentregaespecifico.value = "";
            }
        </script>
    </head>

    <body>

            <?
            extract($_POST);
            extract($_GET);
            //$base= "ucg - copia";
            //$conexion = mysql_connect("localhost","root","");
            //mysql_select_db($base,$conexion);
            include 'conexion.php'
                    ?>

        <form action="ordencompra.php" method="post" name="form1" target="_self" class="Estilo1">

            <table width="1000" border="0" align="center" bordercolor="#000000" class="tablainstitucion">
                <tr>
                    <th colspan="5">ORDENES DE COMPRA, SERVICIO, INTERNA Y ADMINISTRACION</th>
                </tr>
                <tr>
                    <td width="22%"><div align="center"></div></td>
                    <td colspan="4"><span class="Estilo2">
                            <input name="cod_orden" type="hidden" class="boton" id="cod_orden" value="0">
                        </span></td>
                </tr>
                <tr>
                    <td><div align="center">Fecha:</div></td>
                    <td colspan="4"><input name="fec" type="text" class="entrada" id="fec" value="<? echo date('Y-m-d')?>" size="11" maxlength="11"></td>
                </tr>
                <tr>
                    <td height="36" colspan="5">Tipo  de Orden:<span class="style2">
                            <select name="tipo" class="combobox" id="tipo">
                                <option value="">SELECCIONE TIPO DE ORDEN</option>
                                <option value="1" selected>Compra</option>
                                <option value="2">Servicio</option>
                                <option value="3">Orden Interna</option>
                                <option value="4">Administración</option>
                            </select>
                        </span></td>
                </tr>
                <tr>
                    <td><div align="center">Empresa:</div></td>
                    <td colspan="4"><span class="style7">

                                <? $sql="Select * from proveedor order by emp";
                                $resultado = mysql_query($sql, $conexion); ?>
                            <select name="emp" class="combobox" id="emp">
                                    <? if ($emp==$emp ) {?>
                                <option value="<? echo "$emp"; ?>"><? echo "$emp";  ?></option>
                                        <? } ?>
                                    <? while ($busqueda = mysql_fetch_array($resultado)) { ?>
                                <option value="<? echo utf8_encode($busqueda[2]);  ?>" ><? echo utf8_encode($busqueda[1]);?></option>
                                        <? } ?>
                            </select>
                            <? echo "<a href='proveedores.php'><img src='prov.jpg' alt='Registrar Proveedor' width='42' height='31' align='absmiddle'> Registrar Proveedor (solo si no existe el proveedor)</a></font>"?>      </td>
                </tr>


                <tr>
                    <td><div align="center">Rif:</div></td>
                    <td colspan="3">&nbsp; </td>
                    <td width="15%">Fondo N&ordm;:
                        <input name="fondo" type="text" class="entrada" id="fondo" size="5" maxlength="5" value="<? echo "$fondo"; ?>"></td>
                </tr>
                <tr>
                    <td><div align="center">Plazo de Entrega</div></td><td colspan="2"><input type="radio" name="plazoentrega" value="1" onClick="habilitar_plazoentregaespecifico(this.form)" />Dias Habiles<input type="radio" name="plazoentrega" value="2" onClick="habilitar_plazoentregaespecifico(this.form)" />Dias Continuos<input type="radio" name="plazoentrega" value="3" onClick="habilitar_plazoentregaespecifico(this.form)" />Meses Habiles<input type="radio" name="plazoentrega" value="4" onClick="habilitar_plazoentregaespecifico(this.form)" />Meses Continuos</td><td><input type="text" maxlength="2" name="plazoentregaespecifico" value="" disabled="disabled" size="11" onclick="limpiar_plazoentregaespecifico(this.form)" onKeyPress="return validarnumero(event)" /></td>
                </tr>
                <tr>
                </tr>
                <tr>
                </tr>
                <tr>
                    <td><div align="center">Forma de Pago: </div><td colspan="2"><input type="radio" name="formapago" value="1" checked />Orden de Pago<input type="radio" name="formapago" value="2" />Directa<input type="radio" name="formapago" value="3" />Crédito<input type="radio" name="formapago" value="4" />Avances<input type="radio" name="formapago" value="5" />Otros</td><td>Lugar de Entrega: <input type="text" name="lugarentrega" value="La Parroquia" readonly="readonly" size="11" /></td>
                </tr>
                <tr>
                </tr>
                <tr>
                </tr>
                <tr>
                    <td><div align="center">Elaborado Por: </div></td>
                    <td width="39%"><div align="center">Revisado Por:</div></td>
                    <td width="15%"><div align="center">Aprobado Por: </div></td>
                    <td width="15%">&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><div align="center">
                                <? $sqlit="Select * from items where nombre_item='Administrador'";
                                $item= mysql_query($sqlit, $conexion);
                                $items = mysql_fetch_array($item);

                                $sqlit2="Select * from items where nombre_item='Director'";
                                $item2= mysql_query($sqlit2, $conexion);
                                $items2 = mysql_fetch_array($item2);

                                $sqlit3="Select * from items where nombre_item='Asistente'";
                                $item3= mysql_query($sqlit3, $conexion);
                                $items3 = mysql_fetch_array($item3);
                                ?>
                            <input name="elab" type="text" class="entrada" id="elab" value="<? echo utf8_encode($items3[1]);?>">
                        </div></td>
                    <td><div align="center">
                            <input name="adm" type="text" class="entrada" id="adm" value="<? echo utf8_encode($items[1]);?>">
                        </div></td>
                    <td><div align="center">
                            <input name="jef" type="text" class="entrada" id="jef" value="<? echo utf8_encode($items2[1]);?>">
                        </div></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><div align="center">Asistenete Administrativo</div></td>
                    <td><div align="center">Administradora de la Unidad </div></td>
                    <td><div align="center">Jefe de la Unidad </div></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="5"></td>
                </tr>

            </table>
            <div align="center">
                <input name="gu" type="submit" class="boton" value="Guardar Orden">
            </div>

                <? if ($gu) {

                    if($tipo=="" or $emp=="" or $adm=="" or $jef=="" or $fondo=="" or $fec=="" or $elab=="" or $plazoentrega == "" or $plazoentregaespecifico == "" or $plazoentregaespecifico == "Especifique" or $formapago == "") { ?>

            <script language="JavaScript" type="text/JavaScript">
                alert('Faltan datos verifica!')
                window.self.location="ordencompra.php"
            </script>


                        <? }
                    else {

                        //$base="ucg - copia";
                        //$conexion=mysql_connect("localhost","root","");
                        //mysql_select_db($base,$conexion);


                        $sqlor="SELECT MAX(cod_ser_com) from compra1 where tipo='$tipo'";
                        $resulor=mysql_query($sqlor,$conexion);
                        $ordeno= mysql_fetch_array($resulor);
                        echo $ordenos=$ordeno[0]+1;

                        $sqlorp="SELECT MAX(cod_c) from compra1";
                        $resulorp=mysql_query($sqlorp,$conexion);
                        $ordenop= mysql_fetch_array($resulorp);
                        $ordenosp=$ordenop[0]+1;



                        $sqlmax="SELECT * from id_compra where cod_c='$ordenosp'";
                        $resulmax=mysql_query($sqlmax,$conexion);
                        $ordmax= mysql_fetch_array($resulmax);

                        if(!$ordmax) { ?>

            <script language="JavaScript" type="text/JavaScript">
                alert('No existen datos guardados para esta orden!')
                window.self.location="ordencompra.php"
            </script>
                            <? } else {

                            mysql_query("INSERT into compra1 VALUES($ordenosp,$ordenos,$tipo,'$fec','$emp','$fondo','$adm','$jef','','','$elab','$plazoentrega',$plazoentregaespecifico,$formapago,'$lugarentrega')",$conexion);

                        }
                        ?>
            <script language="JavaScript" type="text/JavaScript">
                alert('Orden Guardada!')
                window.self.location="ordencompra.php"
            </script>
                        <?

                    }
                }


                ?>
        </form>
        <div align="center"><a href="index1.php">Menu principal</a>&nbsp;&nbsp;<a href="consulta_compra_gen0.php">Consultar Modificar y Eliminar</a></div>

            <?

            $sqlp="Select * from cuenta_gen group by detalle";
            $resultadop = mysql_query($sqlp, $conexion);

            $sql2="Select * from medida where nombre='IVA'";
            $resultado2 = mysql_query($sql2, $conexion);
            $medidas2 = mysql_fetch_array($resultado2);

            $comc="SELECT cod_c from compra1 where tipo='$tipo' and cod_ser_com='$orden1'";
            $comprc = mysql_query($comc, $conexion);
            $comprac = mysql_fetch_array($comprc);
            $orden=$comprac[0];
            ?>
        <form name="form1" method="post" action="insertar_compra_detalle.php">

            <table width="612" border="0" align="center">
                <tr>
                    <td width="11%" height="39"><div align="center">Cantidad</div></td>
                    <td><div align="center">Unidad de Medida</div></td>
                    <td width="28%"><div align="center">Descripci&oacute;n</div></td>
                    <td width="8%"><div align="center">Excento</div></td>
                    <td width="14%"><div align="center">Precio Unitario</div></td>
                </tr>
                <?php
                    $query = 'select * from medidas_producto';
                    $queryejecuta = mysql_query($query, $conexion);
                ?>
                <tr>
                    <td><div align="center">
                            <input name="can" type="text" class="entrada" id="can" size="9">
                        </div></td>
                        <td><div align="center">
                                <select name="unidadmedida" class="combobox" id="cuenta">
                                    <option value="">SELECCIONE</option>
                                    <? while ($array = mysql_fetch_row($queryejecuta)) { ?>
                                    <option value="<? echo $array[1]; ?>"><? echo utf8_encode($array[1]); ?> </option>
                                            <? }  ?>
                                </select>
                        </div></td>
                    <td><div align="center">
                            <input name="des" type="text" class="entrada" id="des" size="25">
                        </div></td>
                    <td align="center" valign="middle"><div align="center">
                            <input name="exc" type="checkbox" class="entrada" id="exc" value="1">
                        </div></td>
                    <td><div align="center">
                            <input name="pu" type="text" class="entrada" id="pu" size="5">
                        </div></td>
                </tr>
                <tr>
                    <td colspan="4"><div align="center">Cuenta Usada </div></td>
                </tr>
                <tr>
                    <td colspan="4"><div align="center"><span class="style7">
                                <select name="cuenta" class="combobox" id="cuenta">
                                    <option value="">SELECIONE UNA CUENTA</option>
                                        <? while ($busqueda = mysql_fetch_array($resultadop)) { ?>
                                    <option value="<? echo utf8_encode($busqueda[0]); ?>"><? echo utf8_encode($busqueda[1]); ?> </option>
                                            <? }  ?>
                                </select>
                            </span></div></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Cuenta IVA Usada</td>
                    <td align="center" valign="middle">IVA del Producto</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><span class="style7">
                                <?  $sqli="Select * from cuenta_iva group by detalle";
                                $resuli = mysql_query($sqli, $conexion); ?>
                            <select name="cuenta_iva" class="combobox" id="cuenta_iva">
                                <option value="">SELECIONE UNA CUENTA</option>
                                    <? while ($busqueda = mysql_fetch_array($resuli)) { ?>
                                <option value="<? echo $busqueda[0]; ?>"><? echo $busqueda[1]; ?> </option>
                                        <? }  ?>
                            </select>
                        </span></td>
                    <td align="center" valign="middle"><div align="center"> <span class="Estilo2"> </span> <span class="style2"> <span class="Estilo2"> </span></span> <span class="style2"><span class="style7">
                                    <input name="iva" type="text" class="entrada" id="iva" value="<? echo $medidas2[1]?>" size="4" maxlength="4">
                                </span> </span> </div></td>
                    <td><div align="center">
                            <input name="Submit" type="submit" class="boton" value="Agregar">
                        </div></td>
                </tr>
            </table>
        </form>
        <table width="100%" border="1" align="center" class="tablainstitucion">
            <tr>
                <td width="105" height="39" align="center" valign="middle"><div align="center">Cantidad</div></td>
                <td width="293" align="center" valign="middle"><div align="center">Descripci&oacute;n</div></td>
                <td width="95" align="center" valign="middle"><div align="center">Excento</div></td>
                <td width="165" align="center" valign="middle"><div align="center">Cuenta Usada </div></td>
                <td width="117" align="center" valign="middle">Precio Unitario</td>
                <td width="105" align="center" valign="middle">Precio Total</td>
                <td width="58" align="center" valign="middle">%</td>
                <td width="58" align="center" valign="middle">Cuenta IVA</td>
                <td width="58" align="center" valign="middle">Iva</td>
                <td width="71" align="center" valign="middle">Deshacer</td>
            </tr>
                <?
                $com="SELECT max(cod_c) from compra1";
                $compr = mysql_query($com, $conexion);
                $compra = mysql_fetch_array($compr);
                $ord=$compra[0]+1;


                if($ord<>"") {
                    $resul = mysql_query("Select * from id_compra where cod_c='$ord' order by id_compra", $conexion);
                    while ($consul = mysql_fetch_array($resul)) {
                        $id_compra=$consul[0];
                        ?>
            <tr>
                <td align="center" valign="middle">&nbsp;<? echo $consul[2]; ?>
                    <div align="center"></div></td>
                <td align="center" valign="middle">&nbsp;<? echo $consul[3]; ?>
                    <div align="center"></div></td>
                <td align="center" valign="middle">&nbsp;<? if($consul[6]=="1") echo "Excento"; else echo "NO Excento"; ?>
                    <div align="center"></div></td>
                <td align="center" valign="middle">&nbsp;<? echo $consul[5]; ?>
                    <div align="center"></div></td>
                <td align="right" valign="middle">&nbsp;<? echo $consul[4]; ?></td>
                <td align="right" valign="middle">&nbsp;<? $pre_tot=$consul[2]*$consul[4];
                                echo $pre_tot; ?></td>
                <td align="center" valign="middle"><? echo $consul[7]; ?></td>
                <td align="center" valign="middle"><? if($consul[8]=="") echo "Excento"; else echo $consul[8];  ?></td>
                <td align="center" valign="middle"><? echo $consul[9];  ?></td>
                <td align="center" valign="middle"><a href="Borrar_orden.php?<? echo 'id_compra='.$id_compra; ?>"><img src="x2.JPG" width="19" height="21" border="0"/></a></td>
            </tr>
                        <?
                        $sub_total=$sub_total+$pre_tot;
                        if($consul[6]=="1") $pre_total2=0; else $pre_total2=$pre_tot;
                        $sub_total2=$sub_total2+$pre_total2;
                        $iva_total=$iva_total+$consul[9];
                        $total=$sub_total+$iva_total;
                    }
                }
                ?>
            <tr>
                <td colspan="5"><div align="right">SUB - TOTAL</div></td>
                <td align="right" valign="middle"><? echo $sub_total; ?></td>
                <td colspan="4">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="5"><div align="right">IVA  % </div></td>
                <td align="right" valign="middle"><? echo $iva_total; ?></td>
                <td colspan="4">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="5"><div align="right">TOTAL</div></td>
                <td align="right" valign="middle"><? echo $total; ?></td>
                <td colspan="4">&nbsp;</td>
            </tr>
        </table>
        <p align="center">
            <input name="menu" type="submit" class="boton" value="Borrar toda la orden" onClick="window.location='Borrar_orden_toda.php?<? echo 'ord='.$ord;  ?>'">
            &nbsp;</p>
        <p align="center" class="style1">

        </p>
    </body>
</html>
    <? } ?>

